package org.lansir.af.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.TranslateAnimation;
import org.lansir.af.R;

/**
 * 实用工具类
 * @author 蓝智宏
 *
 */
public class Utils {
	private Context ctx = null;

	public static int ANIM_ALPHA_DURATION = 100;
	public static int ANIM_TRANSLATE_DURATION = 30;

	public static int MSGTYPE_DEFAULT = 0;
	public static int MSGTYPE_INFO = 1;
	public static int MSGTYPE_WARNING = 2;
	public static int MSGTYPE_ERROR = 3;
	
	public static Integer informdbType = -1;
	
	public static String sMessage = "";

	public static List<Map<String, Object>> chosenPeople = null;
	
	private SimpleDateFormat dateFormatWeekDay = new SimpleDateFormat("EEEE");
	private SimpleDateFormat dateFormatMonth = new SimpleDateFormat("MMMM");
	private SimpleDateFormat dateFormatLong = new SimpleDateFormat(
			"yyyy-MM-dd, EEEE");
	private SimpleDateFormat dateFormatShort = new SimpleDateFormat(
			"yyyy-MM-dd");
	private SimpleDateFormat dateFormatSql = new SimpleDateFormat(
			"dd-MM-yyyy kk:mm.ss");
	private SimpleDateFormat dateFormatMinute = new SimpleDateFormat(
	"dd-MM-yyyy kk:mm");
	private SimpleDateFormat dateFormatTime = new SimpleDateFormat("kk:mm");

	// UTILS
	public Utils(Context context) {
		ctx = context;
	}
	
	public String getWeekDay(Calendar date) {
		return dateFormatWeekDay.format(date.getTime());
	}
	
	public String getTime(Calendar date){
		return dateFormatTime.format(date.getTime());
	}
	
	public String getMinuteDate(Calendar date){
		return dateFormatMinute.format(date.getTime());
	}

	public String getMonth(Calendar date) {
		return dateFormatMonth.format(date.getTime());
	}

	public String getLongDate(Calendar date) {
		return dateFormatLong.format(date.getTime());
	}

	public String getShortDate(Calendar date) {
		return dateFormatShort.format(date.getTime());
	}

	// WARNING: String.format is VERY SLOW, not for paint draw !
	public String getLongTime(Calendar date, boolean b24HourMode) {
		String s = "";
		if (b24HourMode) {
			s = String.format("%tk:%tM", date, date);
		} else {
			s = String.format("%tk:%tM", date, date);
			if (date.get(Calendar.AM_PM) == 0) // AM
				s = String.format("%tl:%tM am", date, date, date
						.get(Calendar.AM_PM));
			if (date.get(Calendar.AM_PM) == 1) // PM
				s = String.format("%tl:%tM pm", date, date, date
						.get(Calendar.AM_PM));
		}
		return s;
	}

	public String getResStr(int id) {
		return ctx.getResources().getString(id);
	}

	public void alert(String msg) {
		int iconId = 0;
		AlertDialog.Builder dlg = new AlertDialog.Builder(ctx);
		dlg.setMessage(msg);
		dlg.setPositiveButton(getResStr(R.string.msgBoxButtonOk), null);
		dlg.setTitle(ctx.getClass().getName().toString());
		dlg.setIcon(iconId);
		dlg.create();
		dlg.show();
	}

	public void alert(int i) {
		Integer ii = i;
		alert(ii.toString());
		
	}
	
	public void showConfirmDialog(String sMsgContent, DialogInterface.OnClickListener positiveListener, DialogInterface.OnClickListener negativeListener){
		String sTitle = getResStr(R.string.app_name);
		int iconId = 0;
		sTitle = getResStr(R.string.msgTypeWarning);
		iconId = R.drawable.msgicon_warning;
		AlertDialog.Builder dlg = new AlertDialog.Builder(ctx);
		dlg.setMessage(sMsgContent);
		dlg.setPositiveButton(getResStr(R.string.msgBoxButtonOk), positiveListener);
		dlg.setNegativeButton(getResStr(R.string.msgBoxButtonCancel), negativeListener);
		dlg.setTitle(sTitle);
		dlg.setIcon(iconId);
		dlg.create();
		dlg.show();
	}
	
	public void showConfirmDialog(int iMsgContent, DialogInterface.OnClickListener positiveListener, DialogInterface.OnClickListener negativeListener){
		String sTitle = getResStr(R.string.app_name);
		int iconId = 0;
		sTitle = getResStr(R.string.msgTypeWarning);
		iconId = R.drawable.msgicon_warning;
		AlertDialog.Builder dlg = new AlertDialog.Builder(ctx);
		dlg.setMessage(getResStr(iMsgContent));
		dlg.setPositiveButton(getResStr(R.string.msgBoxButtonOk), positiveListener);
		dlg.setNegativeButton(getResStr(R.string.msgBoxButtonCancel), negativeListener);
		dlg.setTitle(sTitle);
		dlg.setIcon(iconId);
		dlg.create();
		dlg.show();
	}
	
	public void showSelectDialog(int iMsgContent, DialogInterface.OnClickListener positiveListener,DialogInterface.OnClickListener neutralListener, DialogInterface.OnClickListener negativeListener){
		String sTitle = getResStr(R.string.app_name);
		int iconId = 0;
		sTitle = getResStr(R.string.msgTypeWarning);
		iconId = R.drawable.msgicon_warning;
		AlertDialog.Builder dlg = new AlertDialog.Builder(ctx);
		dlg.setMessage(getResStr(iMsgContent));
		dlg.setPositiveButton(getResStr(R.string.msgBoxButtonYes), positiveListener);
		dlg.setNeutralButton(getResStr(R.string.msgBoxButtonNo), neutralListener);
		dlg.setNegativeButton(getResStr(R.string.msgBoxButtonCancel), negativeListener);
		dlg.setTitle(sTitle);
		dlg.setIcon(iconId);
		dlg.create();
		dlg.show();
	}

	public void showMsgResStr(int i, int iMsgType) {
		String sTitle = getResStr(R.string.app_name);
		int iconId = 0;
		if (iMsgType == MSGTYPE_INFO) {
			sTitle = getResStr(R.string.msgTypeInfo);
			iconId = R.drawable.msgicon_info;
		}
		if (iMsgType == MSGTYPE_WARNING) {
			sTitle = getResStr(R.string.msgTypeWarning);
			iconId = R.drawable.msgicon_warning;
		}
		if (iMsgType == MSGTYPE_ERROR) {
			sTitle = getResStr(R.string.msgTypeError);
			iconId = R.drawable.msgicon_error;
		}
		AlertDialog.Builder dlg = new AlertDialog.Builder(ctx);
		dlg.setMessage(getResStr(i));
		dlg.setPositiveButton(getResStr(R.string.msgBoxButtonOk), null);
		dlg.setTitle(sTitle);
		dlg.setIcon(iconId);
		dlg.create();
		dlg.show();
	}
	
	public void showMsgResStr(String msg, int iMsgType) {
		String sTitle = getResStr(R.string.app_name);
		int iconId = 0;
		if (iMsgType == MSGTYPE_INFO) {
			sTitle = getResStr(R.string.msgTypeInfo);
			iconId = R.drawable.msgicon_info;
		}
		if (iMsgType == MSGTYPE_WARNING) {
			sTitle = getResStr(R.string.msgTypeWarning);
			iconId = R.drawable.msgicon_warning;
		}
		if (iMsgType == MSGTYPE_ERROR) {
			sTitle = getResStr(R.string.msgTypeError);
			iconId = R.drawable.msgicon_error;
		}
		AlertDialog.Builder dlg = new AlertDialog.Builder(ctx);
		dlg.setMessage(msg);
		dlg.setPositiveButton(getResStr(R.string.msgBoxButtonOk), null);
		dlg.setTitle(sTitle);
		dlg.setIcon(iconId);
		dlg.create();
		dlg.show();
	}

	// sql format: "dd-MM-yyyy kk:mm.ss"
	public static Calendar qqlStrToDate(String s, Calendar dateOut,
			Calendar dateFail) {
		if (s.length() == 19) {
			int dd = Integer.parseInt(s.substring(0, 2));
			int MM = Integer.parseInt(s.substring(3, 5));
			int yyyy = Integer.parseInt(s.substring(6, 10));
			int kk = Integer.parseInt(s.substring(11, 13));
			int mm = Integer.parseInt(s.substring(14, 16));
			int ss = Integer.parseInt(s.substring(17, 19));
			dateOut.set(yyyy, MM - 1, dd, kk, mm, ss);
			return dateOut;
		}
		return dateFail;
	}

	
	public String dateToSqlStr(Calendar date) {
		return dateFormatSql.format(date.getTime());
	}
	
	public static int getTimeAsSeconds(Calendar date) {
		return (date.get(Calendar.HOUR_OF_DAY) * 3600)
				+ date.get(Calendar.MINUTE) * 60;
	}

	public static void clearCalendarTime(Calendar cal) {
		cal.clear(Calendar.MILLISECOND);
		cal.clear(Calendar.SECOND);
		cal.clear(Calendar.MINUTE);
		cal.clear(Calendar.HOUR_OF_DAY);
	}

	public static boolean yearDaysEqual(Calendar calDate, Calendar calDateTo) {
		if (calDate.get(Calendar.YEAR) == calDateTo.get(Calendar.YEAR))
			if (calDate.get(Calendar.MONTH) == calDateTo.get(Calendar.MONTH))
				if (calDate.get(Calendar.DAY_OF_MONTH) == calDateTo
						.get(Calendar.DAY_OF_MONTH))
					return true;
		return false;
	}

	public static boolean yearDaysGreater(Calendar calDate, Calendar calDateTo) {
		if (calDate.get(Calendar.YEAR) >= calDateTo.get(Calendar.YEAR))
			if (calDate.get(Calendar.MONTH) >= calDateTo.get(Calendar.MONTH))
				if (calDate.get(Calendar.DAY_OF_MONTH) >= calDateTo
						.get(Calendar.DAY_OF_MONTH))
					return true;
		return false;
	}

	// compare time: for reminder to show
	public static boolean isTimeOverdued(Calendar calDate, Calendar calDueDate) {
		if ((calDueDate.compareTo(calDate) == 0)
				|| (calDueDate.compareTo(calDate) == 1))
			return true;
		return false;
	}

	// compare time: for calendar view display
	public static boolean isInTimeRange(Calendar calDateStart,
			Calendar calDate, int iDurationInMinutes) {
		if (calDate.get(Calendar.HOUR_OF_DAY) == calDateStart
				.get(Calendar.HOUR_OF_DAY))
			if (calDate.get(Calendar.MINUTE) >= calDateStart
					.get(Calendar.MINUTE))
				if (calDate.get(Calendar.MINUTE) <= (calDateStart
						.get(Calendar.MINUTE) + iDurationInMinutes))
					return true;
		return false;
	}

	// example key: 200712122359
	public static long getDateTimeKey(Calendar calDate) {
		long lYear = calDate.get(Calendar.YEAR) * 100000000;
		long lMonth = calDate.get(Calendar.MONTH) * 1000000;
		long lDay = calDate.get(Calendar.DAY_OF_MONTH) * 10000;
		long lHour = calDate.get(Calendar.HOUR_OF_DAY) * 100;
		long lMinute = calDate.get(Calendar.MINUTE);
		return lYear + lMonth + lDay + lHour + lMinute;
	}

	public static String capitalizeFirstLetter(String sText) {
		return sText.substring(0, 1).toUpperCase()
				+ sText.substring(1, sText.length()).toLowerCase();
	}

	public static String getAppVersionName(Context ctx) {
		try {
			PackageInfo pi = ctx.getPackageManager().getPackageInfo(
					"org.lansir.af", 0);
			return pi.versionName;
		} catch (NameNotFoundException e) {
		}
		return "";
	}

	public static void startAlphaAnimIn(View view) {
		AlphaAnimation anim = new AlphaAnimation(0.5F, 1);
		anim.setDuration(ANIM_ALPHA_DURATION);
		anim.startNow();
		view.startAnimation(anim);
	}

	public static void startTranslateAnimIn(View view) {
		TranslateAnimation anim = new TranslateAnimation(0, 0, -view
				.getHeight(), 0);
		anim.setDuration(ANIM_TRANSLATE_DURATION);
		anim.startNow();
		view.startAnimation(anim);
	}

}
